package com.cloudptt.wp.ui.more;
//Download by htp://www.codefans.net


import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudptt.api.comm.CheckData;
import com.cloudptt.api.comm.CommDlg;
import com.cloudptt.api.product.ISysOperator;
import com.cloudptt.api.product.Product;
import com.cloudptt.api.product.ProductConst;
import com.cloudptt.api.product.func.sys.fsm.SysConst;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.api.product.operator.MsgOperator;
import com.cloudptt.wp.R;
import com.cloudptt.wp.ScreenManager;
import com.cloudptt.wp.SysApplication;
import com.cloudptt.wp.ui.HomeActivity;
import com.cloudptt.wp.ui.Login;
import com.cloudptt.wp.ui.UiDlg;
import com.mcptt.config.local.SysConfig;
import com.mcptt.config.model.BootStrapSvrInfo;
import com.mcptt.db.model.AddrInfo;

public class ServerInfoActivity extends Activity implements OnClickListener {
	public static final String KEY = "isModify";
	private SysConfig config = null;
	private ISysOperator operator = null;
	private EditText ipEdit = null; // 鐎靛棛鐖滅紓鏍帆濡楋拷
	private EditText portEdit = null; // 鐎靛棛鐖滅紓鏍帆濡楋拷
	private String curIp = null;
	private int curPort = 8380;
	private boolean isServerIpChange = false;
	private Button modifyBtn = null;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		Window window = getWindow();
		window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
		window.setStatusBarColor(getResources().getColor(R.color.app_theme_color_primary));
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_bootstrap);
		boolean isModify = getIntent().getBooleanExtra(KEY,false);
		
		getActionBar().setBackgroundDrawable(getResources().getDrawable(R.color.app_theme_color_primary));
		getActionBar().hide();

		ImageView iv = (ImageView) findViewById(R.id.back_up_icon);
		iv.setOnClickListener(this);
		TextView tv = (TextView) findViewById(R.id.title);
		tv.setText(R.string.title_config_server);

		ipEdit = (EditText) findViewById(R.id.ipEditText);
		portEdit = (EditText) findViewById(R.id.portEditText);
		modifyBtn = (Button)findViewById(R.id.modifyBtn);
		modifyBtn.setOnClickListener(this);
		if(!isModify)
		{
			ipEdit.setEnabled(false);
			portEdit.setEnabled(false);
			modifyBtn.setVisibility(View.GONE);
		}
		ScreenManager.getScreenManager().pushActivity(this);
	}



	@Override
	protected void onResume() {
		super.onResume();
		SysApplication app = ((SysApplication) getApplicationContext());
		Product product = app.getProduct();
		if(product != null) {
			this.operator = (ISysOperator) product.getSysOperator();
			//		product.checkCurVersion(this,false,true,SysApplication.APP_KEY);

			this.config = product.getSysConfig();
			if (config != null) {
				BootStrapSvrInfo svrInfo = config.getBootStrapSvr();
				curIp = svrInfo.getServerIp();
				curPort = svrInfo.getPort();
				Log.debug(getClass(), "onResume ipStr = " + curIp + " port = " + curPort);
				ipEdit.setText(curIp);
				portEdit.setText("" + curPort);
			}
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		
		if(android.R.id.home == item.getItemId()){
			finish();
			return true;
		}
		
		return super.onOptionsItemSelected(item);
	}
	
	public void onClick(View v) {
		int id = v.getId();
		if(id == R.id.back_up_icon) 
		{
			this.finish();
		}
		else if(id == R.id.modifyBtn)
		{
			doModifyAction();
		}
	}

	public void doModifyAction()
	{
		if (this.operator == null
				|| config == null)
		{
			CommDlg.showToast(ServerInfoActivity.this, getString(R.string.login_failed_network));
			return;
		}

		String ipStr = this.ipEdit.getText().toString().trim();
		String str = this.portEdit.getText().toString().trim();

		if(!CheckData.checkIP(ipStr))
		{
			CommDlg.show(this,getString(R.string.ip_err));
			return;
		}
		if(!CheckData.checkPort(str))
		{
			CommDlg.show(this,getString(R.string.port_err));
			return;
		}
		int val = CheckData.parseInt(str);
		if (ipStr.length()<1 || ipStr.length()<1 )
		{
			return;
		}
		Log.debug(this.getClass(), "doLoginAction ipStr =  " + ipStr + " val = " + val);
		Log.debug(this.getClass(), "doLoginAction this.ipStr =  " + this.curIp + " port = " + curPort);
		config.setBootStrapSvrInfo(ipStr, val);
		if(!curIp.equals(ipStr)
				|| curPort != val)
		{
			isServerIpChange = true;
		}
		Log.debug(this.getClass(), "doLoginAction  isServerIpChange = " + isServerIpChange);
		if(isServerIpChange) {

			DialogInterface.OnClickListener okListener = new DialogInterface.OnClickListener()
			{
				public void onClick(DialogInterface dialog, int which)
				{
					doExitAction();
				}
			};
			String tipStr = getString(R.string.bootstrup_modify);
			CommDlg.showConfirmDlg(this, tipStr, okListener);
		}
		else
		{
			ScreenManager.getScreenManager().popAllActivityExceptOne(Login.class);
		}
	}

	private void doExitAction(){
		Log.debug(this.getClass(), "doExitAction");
		SysApplication app = ((SysApplication) getApplicationContext());
		Product product = app.getProduct();
		product.doExit();

		SharedPreferences sharedPreferences = getSharedPreferences(HomeActivity.KEY_HOME, Context.MODE_PRIVATE);
		sharedPreferences.edit().putBoolean(HomeActivity.KEY_NAME_ISFIRST, false).commit();

		ScreenManager.getScreenManager().popAllActivity();
		SystemClock.sleep(2000);
		android.os.Process.killProcess(android.os.Process.myPid());
		System.exit(0);
	}
	public void login_back(View v) {     
		this.finish();
	}  
	
}
